<?php

/**
 * Controller_district
 * 卫生地段负责表
 */

class Controller_district extends MyApp {

	public function __construct()
	{

		parent::__construct();
		
		$this->model->semester = YOU::loadModel('semester');
		$this->model->class    = YOU::loadModel('class');
		$this->model->log      = YOU::loadModel('log');

	}	
    /** 首页 **/
    public function actionIndex()
    {

    }

	/** 卫生地段负责列表 **/
    public function actionList()
    {   
    	// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 获取当前学期id
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];
        $where = "";
		// 获取所有的学期
		$semester_lists = $this->model->semester->getAll();

		// 卫生地段负责说明和区域
		$explain_data = $this->db->fetchRow("SELECT * FROM `tbl_sanitation_explain` WHERE 1=1 {$where}");
		// 搜索
		$semester_id	= (int)$this->get->semester_id;



		if (!empty($semester_id)){
			$where = " AND a.semester_id ={$semester_id} ";
			$this->tpl->assign("semester_id", $semester_id);
		}
		// 卫生地段负责说明和区域
		$explain_data = $this->db->fetchRow("SELECT * FROM `tbl_sanitation_explain` AS a WHERE 1=1 {$where}");
		// 学期标题名称
		$semester_data = $this->db->fetchRow("SELECT * FROM `tbl_semester` AS a WHERE 1=1 {$where}");
		$semester_data['semester_name'] =$this->semester_name[$semester_data['semester_name']];
        // 分页     
        YOU::loadLib('page', "", '0');
        $tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_sanitation_district` AS a,`tbl_class_up` AS b WHERE b.is_now =1 AND a.class_id = b.class_id {$where}");
        $count = $tmp['count'];
        $page_total = ceil($count/PAGE_LIMIT);
        // 当前页                  
        $p = intval($_GET['p']);
        if ($p == 0){
            $p  = 1;
        }
        if ($p > 1){	
            $start = PAGE_LIMIT * ($p-1);
        }else{
            $start = 0;
        }

        $district_lists = $this->db->fetchAll("SELECT a.*,b.* FROM `tbl_sanitation_district` AS a,`tbl_class_up` AS b WHERE b.is_now =1 AND a.class_id = b.class_id {$where} ORDER BY district_id  LIMIT {$start}, ".PAGE_LIMIT."");

        $page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
        $page->next_page   = '下一页>';
        $page->pre_page    = '<上一页';
        $page->style_path  = "static/page_style";
       
	    $this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("district_lists", $district_lists);
		$this->tpl->assign("explain_data", $explain_data);
	
		$this->tpl->assign("semester_lists", $semester_lists);
        $this->tpl->display('district/list.tpl.php');
    }
	/** 卫生地段批量录入 **/
    public function actionAdd()
    {        
		// 获取当前学期id
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];
		$semester_data = $this->model->semester->getInfo($semester_id);
		$semester_data['semester_name'] =$this->semester_name[$semester_data['semester_name']];		// 班级
		$class_lists = $this->model->class->getAll();
	
        if ($_POST){ 

			$explain_name   	= $this->post->explain_name;
			$explain_content   	= $this->post->explain_content;
			// 常规地段，其他地段
			foreach($class_lists as $key=>$value){
				$district_rule   	= $_POST["district_rule_{$value['class_id']}"];
				$district_rests   	= $_POST["district_rests_{$value['class_id']}"];				
				$district_rule		= $district_rule['0'];
				$district_rests		= $district_rests['0'];
				// 全部为空不写入数据库
				if(!empty($district_rule) || !empty($district_rests)){

					// 写入数据库
					$info = array( 
					
					"semester_id"				=> $semester_id,
					"class_id"					=> $value['class_id'],
					"district_rule"				=> $district_rule,
					"district_rests"			=> $district_rests,
					"status"					=> 1,
					"create_time"				=> date("Y-m-d H:i:s"),
					"update_time"				=> "0000-00-00 00:00:00",  

					 );
					
					$this->db->do_insert("tbl_sanitation_district", $info);
					}
				}
			
			// 说明，区域部分
			if (!empty($explain_name) && !empty($explain_content)){
				$explain_data = $this->db->fetchRow("SELECT * FROM `tbl_sanitation_explain` WHERE semester_id={$semester_id}");
				
				// 说明、区域名称写入数据库
				$info = array( 
					
				"semester_id"			=> $semester_id,
				"explain_name"			=> $explain_name,
				"explain_content"		=> $explain_content,
				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",  

				 );
				// 本学期的卫生地段说明已经存在就不增加而是修改
				if(!empty($explain_data)){
					$this->db->do_update("tbl_sanitation_explain", $info, "semester_id='{$semester_id}'");
				}else{
				
				$this->db->do_insert("tbl_sanitation_explain", $info);
				
				}
			}

            
          Helper_Client::location("添加成功", "index.php?a=district.list");

         }
		 $this->tpl->assign("semester_data", $semester_data);
		  $this->tpl->assign("class_lists", $class_lists);
         $this->tpl->display("district/add.tpl.php");
    }
	 /** 卫生地段修改 **/
    public function actionUpdate()
    {   
    	// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		

        $district_id	= (int)$this->get->district_id;
		$district_lists = $this->db->fetchRow("SELECT * FROM `tbl_sanitation_district` AS a ,`tbl_class_up` AS b  WHERE b.is_now =1 AND a.class_id=b.class_id AND  a.district_id={$district_id}");
	
	    if (empty($district_lists)){
            Helper_Client::alert("信息不存在");
        }

        if ($_POST){ 

			$district_rule   		= $this->post->district_rule;
			$district_rests   		= $this->post->district_rests;

            // 写入数据库
            $info = array(
				
			"district_rule"			=> $district_rule,
			"district_rests"		=> $district_rests,
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",  

			 );

          $this->db->do_update("tbl_sanitation_district", $info, "district_id='{$district_id}'");

          // 跳转
          Helper_Client::location("修改成功", "index.php?a=district.list");

         }

		$this->tpl->assign("district_lists", $district_lists);
	    $this->tpl->display("district/update.tpl.php");
    } 
	/** 卫生地段删除 **/
    public function actionDel()
    {
        $district_id = $this->get->district_id;   
        $this->db->query("DELETE  FROM `tbl_sanitation_district` WHERE district_id='{$district_id}'");
         // 跳转
        Helper_Client::location("删除成功", "index.php?a=district.list");
    }
}

?>